<template>
    <div class="search-box">
        <input ref="query" v-model="query" class="box" :placeholder="placeholder"  />
        <i @click="clear" v-show="query" class="fa fa-times-circle clear"></i>
        <div class="btn" @click="search"><i class="fa fa-search"></i></div>
    </div>
</template>

<script>
export default {
  props: {
    placeholder: {
      type: String,
      default: "输入搜索内容"
    }
  },
  data() {
    return {
      query: ""
    };
  },
  methods: {
    clear() {
      this.query = "";
    },
    setQuery(query) {
      this.query = query;
    },
    blur() {
      this.$refs.query.blur();
    },
    search(){
        this.$emit('search')
    }
  },
  // created() {
  //     this.$watch('query',(newQuery)=>{
  //         this.$emit('query',newQuery)
  //     })
  // },
  watch: {
    query(newQuery) {
      this.$emit("query", newQuery);
    }
  }
};
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
@import '~common/stylus/variable';

.search-box {
    display: flex;
    align-items: center;
    box-sizing: border-box;
    width: 100%;
    padding: 0 6px;
    height: 40px;
    position: relative;

    .box {
        flex: 1;
        height: 40px;
        border: 1px solid $color-theme;
        box-sizing: border-box;
        padding:0 30px 0 10px;
        border-radius: 5px 0 0 5px;
    }

    .clear {
        position: absolute;
        right: 75px;
        font-size: $font-size-large;
    }

    .btn {
        width: 60px;
        color: #ffffff;
        background: $color-theme;
        height: 40px;
        line-height: 40px;
        border-radius: 0 5px 5px 0;
        i {
            font-size $font-size-large
        }
    }
}
</style>
